package top.xiaoshuaila.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.pojo.Admin;
import top.xiaoshuaila.service.IAdminService;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private IAdminService adminService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //重写loadUserByusername通过username获取对应的继承了userdetail接口的admin对象
        Admin admin =
                adminService.getAdminByUserName(username);
        if(null!=admin)
            admin.setRoles(adminService.getRoles(admin.getId()));
        if(admin!=null)return admin;
        throw new UsernameNotFoundException("用户名或者密码不正确");
    }
}